package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品{}",dishDTO);
        dishService.insert(dishDTO);
        //删除受影响的套餐缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        clearCache(key);
        log.info("清除缓存{}",key);
        return Result.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("查询菜品：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping
    //@RequestParam用于解析参数
    public Result deleteById(@RequestParam List<Long> ids){
        log.info("批量删除菜品{}",ids);
        dishService.deleteBatch(ids);
        //删除受影响的缓存
        String key = "dish_*";
        clearCache(key);
        log.info("清除缓存{}",key);
        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> selectById(@PathVariable Long id){
        log.info("查询菜品{}",id);
        DishVO dishVO = dishService.selectById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品信息")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息{}",dishDTO);
        dishService.update(dishDTO);
        //删除受影响的缓存
        String key = "dish_*";
        clearCache(key);
        log.info("清除缓存{}",key);

        return Result.success();
    }

    @ApiOperation("停售启售菜品")
    @PostMapping("/status/{status}")
    public Result stopOrStart(@PathVariable Integer status,@RequestParam Long id){
        log.info("修改id为：{}的菜品的状态为{}",id,status);
        dishService.stopOrStart(status,id);
        //删除受影响的缓存
        String key = "dish_*";
        clearCache(key);
        log.info("清除缓存{}",key);
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> selectByCategory(@RequestParam Long categoryId){
        log.info("查询分类id为{}的菜品",categoryId);
        List<Dish> dishes = dishService.selectByCategory(categoryId);
        return Result.success(dishes);
    }

    /***
     * 清理缓存数据
     * @param pattern
     */
    private void clearCache(String pattern){
        //查询符合条件的key
        Set keys = redisTemplate.keys(pattern);
        //删除对应的缓存数据
        redisTemplate.delete(keys);
    }

}
